<?php

/*
 *	Application namespace
 */
namespace Application\Controllers
{
	/*
	 *	AJAX controller
	 */
	final class Ajax
	{
		/*
		 *	Constructor
		 */
		public function __construct(){}
		
		/*
		 *	Disable caching for AJAX
		 */
		private static function __caching()
		{
			\Nano\Headers::add('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
			\Nano\Headers::add('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); 
			\Nano\Headers::add('Cache-Control: no-store, no-cache, must-revalidate');
			\Nano\Headers::add('Pragma: no-cache');
		}
		
		/*
		 *	Print some message
		 */
		private static function __print($data)
		{
			print($data);
			
			\Nano\Output::getInstance()->flush();
			
			exit();
		}
		
		/*
		 *	Index method
		 */
		public function __index($params = array())
		{
			// Set headers
			self::__caching();
			
			// Is it real XMLHTTPRequest?
			if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
			{
				// Print "Hello, world!" msg
				self::__print(\Nano\JSON::ping('Hello, world!'));
			}
			
			// Or not?
			else
			{
				self::__print(\Nano\JSON::error('Invalid XHR request'));
			}
		}
	}
}
